# -*- coding:utf-8 -*-
import cv2
import networkx as nx
import matplotlib.pyplot as plt

# 创建DAG
G = nx.DiGraph()

# 顶点列表
vertex_list = ['v'+str(i) for i in range(1, 22)]
# 添加顶点
G.add_nodes_from(vertex_list)

# 边列表
edge_list = [
             ('v1', 'v5'), ('v1', 'v6'), ('v1', 'v7'),('v1', 'v8'),('v1', 'v9'),
             ('v2', 'v5'), ('v2', 'v6'), ('v2', 'v7'),('v2', 'v8'),('v2', 'v9'),
             ('v3', 'v5'), ('v3', 'v6'), ('v3', 'v7'),('v3', 'v8'),('v3', 'v9'),
             ('v4', 'v5'), ('v4', 'v6'), ('v4', 'v7'),('v4', 'v8'),('v4', 'v9'),
             ('v5','v10'),('v5','v11'),('v5','v12'),('v5','v13'),('v5','v14'),('v5','v15'),
             ('v6','v10'),('v6','v11'),('v6','v12'),('v6','v13'),('v6','v14'),('v6','v15'),
             ('v7','v10'),('v7','v11'),('v7','v12'),('v7','v13'),('v7','v14'),('v7','v15'),
             ('v8','v10'),('v8','v11'),('v8','v12'),('v8','v13'),('v8','v14'),('v8','v15'),
             ('v9','v10'),('v9','v11'),('v9','v12'),('v9','v13'),('v9','v14'),('v9','v15'),
             ('v10','v16'),('v10','v17'),('v10','v18'),
             ('v11','v16'),('v11','v17'),('v11','v18'),
             ('v12','v16'),('v12','v17'),('v12','v18'),
             ('v13','v16'),('v13','v17'),('v13','v18'),
             ('v14','v16'),('v14','v17'),('v14','v18'),
             ('v15','v16'),('v15','v17'),('v15','v18'),
             ('v16','v19'),
             ('v17','v20'),
             ('v18','v21')
            ]
# 通过列表形式来添加边
G.add_edges_from(edge_list)

# 指定绘制DAG图时每个顶点的位置
pos = {
        'v1':(-2,1.5),
        'v2':(-2,0.5),
        'v3':(-2,-0.5),
        'v4':(-2,-1.5),
        'v5':(-1,2),
        'v6': (-1,1),
        'v7':(-1,0),
        'v8':(-1,-1),
        'v9':(-1,-2),
        'v10':(0,2.5),
        'v11':(0,1.5),
        'v12':(0,0.5),
        'v13':(0,-0.5),
        'v14':(0,-1.5),
        'v15':(0,-2.5),
        'v16':(1,1),
        'v17':(1,0),
        'v18':(1,-1),
        'v19':(2,1),
        'v20':(2,0),
        'v21':(2,-1)
       }
# 绘制DAG图
plt.title('DNN for iris')    #图片标题
plt.xlim(-2.2, 2.2)                     #设置X轴坐标范围
plt.ylim(-3, 3)                     #设置Y轴坐标范围
nx.draw(
        G,
        pos = pos,                      # 点的位置
        node_color = 'red',             # 顶点颜色
        edge_color = 'black',           # 边的颜色
        font_size =10,                  # 文字大小
        node_size =300                  # 顶点大小
       )

# 保存图片，图片大小为640*480
# plt.savefig('E://data/DNN_sketch.png')
plt.show()
# 利用opencv模块对DNN框架添加文字注释

# 读取图片
# imagepath = 'E://data/DNN_sketch.png'
# image = cv2.imread(imagepath, 1)

# # 输入层
# cv2.rectangle(image, (85, 130), (120, 360), (255,0,0), 2)
# cv2.putText(image, "Input Layer", (15, 390), 1, 1.5, (0, 255, 0), 2, 1)

# # 隐藏层
# cv2.rectangle(image, (190, 70), (360, 420), (255,0,0), 2)
# cv2.putText(image, "Hidden Layer", (210, 450), 1, 1.5, (0, 255, 0), 2, 1)

# # 输出层
# cv2.rectangle(image, (420, 150), (460, 330), (255,0,0), 2)
# cv2.putText(image, "Output Layer", (380, 360), 1, 1.5, (0, 255, 0), 2, 1)

# # sofrmax层
# cv2.rectangle(image, (530, 150), (570, 330), (255,0,0), 2)
# cv2.putText(image, "Softmax Func", (450, 130), 1, 1.5, (0, 0, 255), 2, 1)

# 保存修改后的图片
# cv2.imwrite('E://data/DNN.png', image)